package com.itfreer.gis.server.storage;

import java.io.IOException;
import java.io.InputStream;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.xmlpull.v1.XmlPullParserException;

import io.minio.MinioClient;
import io.minio.Result;
import io.minio.errors.ErrorResponseException;
import io.minio.errors.InsufficientDataException;
import io.minio.errors.InternalException;
import io.minio.errors.InvalidBucketNameException;
import io.minio.errors.InvalidEndpointException;
import io.minio.errors.InvalidObjectPrefixException;
import io.minio.errors.InvalidPortException;
import io.minio.errors.NoResponseException;
import io.minio.errors.RegionConflictException;
import io.minio.messages.Item;
import io.minio.policy.PolicyType;

/**
 * 定义切片存储器
 */
@Component
public class MinIOStorage {

	@Autowired
	private StoragePara storagePara;

	private MinioClient minioClient;

	public MinIOStorage() {

	}

	/**
	 * 存储对象
	 * 
	 * @param bucketName
	 * @param objectName
	 * @param stream
	 * @param contentType
	 * @throws Exception
	 */
	public void putObject(String bucketName, String objectName, InputStream stream, String contentType)
			throws Exception {
		if (minioClient == null) {
			minioClient = new MinioClient(storagePara.getEndpoint(), storagePara.getAccessKey(),
					storagePara.getSecretKey());
		}
		minioClient.putObject(bucketName, objectName, stream, contentType);
	}

	/**
	 * @param bucketName 桶名
	 * @param prefix     前缀
	 * @return 目录下的文件信息列表
	 */
	public List<Map<String, Object>> listObjects(String bucketName, String prefix)
			throws InvalidPortException, InvalidEndpointException, XmlPullParserException, InsufficientDataException,
			NoSuchAlgorithmException, IOException, NoResponseException, InvalidKeyException, InternalException,
			InvalidBucketNameException, ErrorResponseException {

		if (minioClient == null) {
			minioClient = new MinioClient(storagePara.getEndpoint(), storagePara.getAccessKey(),
					storagePara.getSecretKey());
		}

		boolean isExist = minioClient.bucketExists(bucketName);
		if (!isExist) {
			try {
				minioClient.makeBucket(bucketName);
				minioClient.setBucketPolicy(bucketName, "*", PolicyType.READ_WRITE);
			} catch (RegionConflictException e) {
				e.printStackTrace();
			} catch (InvalidObjectPrefixException e) {
				e.printStackTrace();
			}
		}

		Iterable<Result<Item>> results = minioClient.listObjects(bucketName, prefix);
		Iterator<Result<Item>> iterator = results.iterator();
		List<Map<String, Object>> list = new ArrayList<>();
		while (iterator.hasNext()) {
			Result<Item> result = (Result<Item>) iterator.next();

			if (result != null) {
				Map<String, Object> map = result.get();
				list.add(map);
			}

		}
		return list;
	}

	/**
	 * 存储对象
	 * 
	 * @param bucketName
	 * @param objectName
	 * @param stream
	 * @param contentType
	 * @throws Exception
	 */
	public void removeObject(String bucketName, String objectName) {
		try {
			if (minioClient == null) {
				minioClient = new MinioClient(storagePara.getEndpoint(), storagePara.getAccessKey(),
						storagePara.getSecretKey());
			}
			minioClient.removeObject(bucketName, objectName);
		} catch (Exception e) {
		}
	}

	public InputStream getObject(String bucketName, String objectName) {
		try {
			if (minioClient == null) {
				minioClient = new MinioClient(storagePara.getEndpoint(), storagePara.getAccessKey(),
						storagePara.getSecretKey());
			}
			return minioClient.getObject(bucketName, objectName);
		} catch (Exception e) {
			return null;
		}
	}
}
